﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SGCG.Model;

namespace SGCG
{
    public partial class MainPage : UserControl
    {
        private readonly PellBitmap bitmap;
        private Actions.Action __currentAction = null;

        public MainPage()
        {
            UserMessage.MainPage = this;

            this.DataContext = this;

            bitmap = new PellBitmap();

            InitializeComponent();
        }

        private void RenderedBitmap_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (CurrentAction != null)
            {
                CurrentAction.HandleClick(e, this.RenderedBitmap);
            }
        }

        private void RenderedBitmap_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (CurrentAction != null)
            {
                CurrentAction.HandleRightClick(e, this.RenderedBitmap);
            }
            e.Handled = true;
        }

        private void RenderedBitmap_MouseMove(object sender, MouseEventArgs e)
        {
            if (CurrentAction != null)
            {
                CurrentAction.HandleMouseMove(e, this.RenderedBitmap);
            }
        }

        private void BotaoDesenharLinha_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentAction = new Actions.CreateLineAction(this.bitmap);
        }

        private void BotaoDesenharCirculo_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentAction = new Actions.CreateCircleAction(this.bitmap);
        }

        public WriteableBitmap Bitmap
        {
            get { return this.bitmap.Bitmap; }
        }

        public FrameworkElement PresentedToolbox
        {
            get { return (FrameworkElement)GetValue(PresentedToolboxProperty); }
            set { SetValue(PresentedToolboxProperty, value); }
        }

        private Actions.Action CurrentAction
        {
            get { return this.__currentAction; }

            set
            {
                if (this.__currentAction != null)
                {
                    this.__currentAction.Cancel();
                }

                this.__currentAction = value;
                this.PresentedToolbox = (FrameworkElement)this.__currentAction.Pallete ?? new Border();
            }
        }

        // Using a DependencyProperty as the backing store for PresentedToolbox.  This enables animation, styling, binding, etc...1+
        public static readonly DependencyProperty PresentedToolboxProperty =
            DependencyProperty.Register("PresentedToolbox", typeof(FrameworkElement), typeof(MainPage), new PropertyMetadata(null));

        private void BotaoSelecionar_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentAction = new Actions.SelectAction(this.bitmap);
        }

        private void BotaoRetangulo_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentAction = new Actions.CreateRectangleAction(this.bitmap);
        }

        private void BotaoTriangulo_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentAction = new Actions.CreateTriangleAction(this.bitmap);
        }

        private void BotaoArco_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentAction = new Actions.CreateArcAction(this.bitmap);
        }

        private void CheckboxDebug_Checked(object sender, RoutedEventArgs e)
        {
            DebugControl.Show();
            UserMessage.IsDebugEnabled = true;
        }

        private void CheckboxDebug_Unchecked(object sender, RoutedEventArgs e)
        {
            DebugControl.Hide();
            UserMessage.IsDebugEnabled = false;
        }

        private void BotaoCurvaLivre_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentAction = new Actions.CreateFreeCurveAction(this.bitmap);
        }

        private void BotaoCurvaLivreContinua_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentAction = new Actions.CreateContinuousFreeCurve(this.bitmap);
        }

        private void BotaoPoligonoRegular_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentAction = new Actions.CreateRegularPolygonAction(this.bitmap);
        }

    }
}
